package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zy.constant.RedisConstant;
import com.zy.dao.SetmealDao;
import com.zy.entity.PageResult;
import com.zy.entity.QueryPageBean;
import com.zy.pojo.Setmeal;
import com.zy.service.CheckGroupService;
import com.zy.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass= SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

	@Autowired
	private SetmealDao setmealDao;

	@Autowired
	private JedisPool jedisPool;


	@Override
	public void add(Integer[] checkgroupIds, Setmeal setmeal) {
		//添加，返回新添加数据的主键
		setmealDao.add(setmeal);
		//得到返回的主键
		Integer setmealId = setmeal.getId();
		//写中间表
		for (Integer checkgroupId : checkgroupIds) {
			setmealDao.setSetmealAndCheckGroup(setmealId,checkgroupId);
		}
		//把添加时用的照片名称放到redis集合中
		jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		PageHelper.startPage(currentPage,pageSize);
		Page<Setmeal> page=setmealDao.selectByCondition(queryString);
		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public List<Integer> findCheckitemAndCheckgroupId(Integer id) {
		return setmealDao.findCheckitemAndCheckgroupId(id);
	}

	@Override
	public String img(Integer id) {
		return setmealDao.img(id);
	}

	@Override
	public void edit(Integer[] checkitemIds, Setmeal setmeal) {

		//在redis用户集合中，把原来图片去除+添加新的图片信息到
//		jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,set)
			//查询原来表中存储的图片名称
		String img = setmealDao.img(setmeal.getId());
			//移除原来的数据
		Jedis resource = jedisPool.getResource();
		resource.srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
			//添加改变的数据
		resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, img);
		resource.close();

		//解除中间表信息
		setmealDao.delSetmealAndCheckgroup(setmeal.getId());
		//修改基本信息
		setmealDao.edit(setmeal);
		//添加中间表信息
		for (Integer checkitemId : checkitemIds) {
			setmealDao.setSetmealAndCheckGroup(setmeal.getId(), checkitemId);
		}
	}

	@Override
	public void delete(Integer id) {
		//先删除redis中的数据
		//查询原来表中存储的图片名称
		String img = setmealDao.img(id);
		//移除原来的数据
		Jedis resource = jedisPool.getResource();
		resource.srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
		resource.close();
		//删除中间表数据
		setmealDao.delSetmealAndCheckgroup(id);
		//删除基础数据
		setmealDao.delSetmealById(id);
	}

	@Override
	public List<Setmeal> getAllSetmeal() {
		return setmealDao.findAll();
	}

	@Override
	public Setmeal findById(Integer id) {
		return setmealDao.findById(id);
	}

	@Override
	public List<Map> findSetmealCount() {
		return setmealDao.findSetmealCount();
	}


}
